﻿/*
    This file is part of nDoctor Light

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace Probel.Ldoc.Dal.Services
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Text;
    using System.Threading.Tasks;

    using AutoMapper;

    using NHibernate;
    using NHibernate.Linq;

    using Probel.Ldoc.Dal.Configuration;
    using Probel.Ldoc.Dal.Entities;
    using Probel.Ldoc.Dal.Helpers;
    using Probel.Ldoc.Services.Dto;
    using Probel.Ldoc.Services.Services;

    internal class PatientSearcher : ISearcher<Patient>
    {
        #region Fields

        private readonly ISession Session;

        #endregion Fields

        #region Constructors

        public PatientSearcher()
        {
            this.Session = DalManager.SessionFactory.OpenSession();
        }

        #endregion Constructors

        #region Methods

        public void Dispose()
        {
            this.Session.Dispose();
        }

        /// <summary>
        /// Finds the patient that fits the criterion. A logical OR will be applied and a String.Contains
        /// </summary>
        /// <param name="criterion">The criterion.</param>
        /// <returns>The found patients</returns>
        ///<remarks>http://stackoverflow.com/questions/9565360/how-to-convert-utf-8-characters-to-ascii-for-use-in-a-url</remarks>
        public IQueryable<Patient> FindContainingOr(SearchCriterion criterion)
        {
            if (criterion.IncludeAll) { return Session.Query<Patient>(); }
            else
            {
                var value = criterion.Criterion.ToLower();
                var predicate = PredicateBuilder.False<Patient>();
                if (criterion.IncludeFirstName) { predicate = predicate.Or<Patient>(p => p.FirstName.ToLower().Contains(value)); }
                if (criterion.IncludeLastName) { predicate = predicate.Or<Patient>(p => p.LastName.ToLower().Contains(value)); }

                return Session.Query<Patient>().Where(predicate);
            }
        }

        /// <summary>
        /// Finds the patient that fits the criterion. A logical OR will be applied and a string.StartsWith
        /// </summary>
        /// <param name="criterion">The criterion.</param>
        /// <returns>The found patients</returns>
        ///<remarks>http://stackoverflow.com/questions/9565360/how-to-convert-utf-8-characters-to-ascii-for-use-in-a-url</remarks>
        public IQueryable<Patient> FindStartWithOr(SearchCriterion criterion)
        {
            if (criterion.IncludeAll) { return Session.Query<Patient>(); }
            else
            {
                var value = criterion.Criterion.ToLower();
                var predicate = PredicateBuilder.False<Patient>();
                if (criterion.IncludeFirstName) { predicate = predicate.Or<Patient>(p => p.FirstName.ToLower().StartsWith(value)); }
                if (criterion.IncludeLastName) { predicate = predicate.Or<Patient>(p => p.LastName.ToLower().StartsWith(value)); }

                return Session.Query<Patient>().Where(predicate);
            }
        }

        #endregion Methods
    }
}